package com.aaa.controller;

import com.aaa.entity.Pager;
import com.aaa.entity.StudentEntity;
import com.aaa.service.StudentService;
import com.aaa.service.StudentServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "StudentServlet",urlPatterns = "/student.action")
public class StudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        if (method!=null)
        {
            switch (method)
            {
                case "add":
                    add(request,response);
                    break;
                case "query":
                    query(request,response);
                    break;
                case "deletestu":
                    deleterole(request,response);
                    break;
                case "updateone":
                    updateone(request,response);
                    break;
            }

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    protected void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageNo = request.getParameter("pageNo")==null?"1":request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize")==null?"1":request.getParameter("pageNo");
        //查询条件
        Map<String,String> condition=new HashMap<>();
        condition.put("stuname", request.getParameter("stuname"));  //
        condition.put("sex", request.getParameter("sex"));
        condition.put("classname", request.getParameter("classname"));
        condition.put("phone", request.getParameter("phone"));
        condition.put("stutate", request.getParameter("stutate"));
        condition.put("remark", request.getParameter("remark"));

        StudentService service=new StudentServiceImpl();
        Pager pager=service.query(Integer.valueOf(pageNo), Integer.valueOf(pageSize), condition);
        response.getWriter().print(JSON.toJSONString(pager));
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String stuid=request.getParameter("stuid");
        String stuname=request.getParameter("stuname");
        String sex=request.getParameter("sex");  //
        String phone=request.getParameter("phone");
        String stutate=request.getParameter("stutate");
        String remark=request.getParameter("remark");

       StudentService service=new StudentServiceImpl();
        StudentEntity info=new StudentEntity(Integer.valueOf(stuid),stuname,sex,phone,stutate,remark);
        int res= service.create(info);
        PrintWriter writer = response.getWriter();
        writer.print(JSON.toJSONString(res));

    }

    protected void deleterole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String stuid=request.getParameter("stuid");
        StudentService service=new StudentServiceImpl();
        int res=service.delete(Integer.parseInt(stuid));
        String str=JSON.toJSONString(res);
        response.getWriter().print(str);
    }
    private void updateone(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        String stuid=request.getParameter("stuid");
        StudentService service=new StudentServiceImpl();
        Map map=service.queryid(Integer.valueOf(stuid));
        String str= JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
        response.getWriter().print(str);
    }
}
